package com.tools.scanner.network

import com.tools.scanner.constant.UrlConstant
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

class RetrofitImpl {
    companion object {
        private const val READ_TIME_OUT = 10000L
        private const val CONNECT_TIME_OUT = 10000L

        private val mHttpClient: OkHttpClient = OkHttpClient.Builder()
            .readTimeout(READ_TIME_OUT, TimeUnit.SECONDS)
            .connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS)
            .addInterceptor(
                HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
            )
            .addInterceptor(ParamsInterceptor())
            .build()

        fun <T> createApiService(service: Class<T>): T {
            val retrofit = Retrofit.Builder()
                .baseUrl(UrlConstant.BASE_URL)
                .client(mHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
            return retrofit.create(service)
        }
    }
}